const { BaseCommand } = require("./base");
const fs = require("node:fs");
const { get_fst_file } = require("../utils/file_tool");
const { hash } = require("../utils/hash_tool");

class HashCommand extends BaseCommand {
    async execute() {
        let target;
        if (this.selfData.content) {
            target = `${this.selfData.content}`;
        } else if (this.selfData.src) {
            const file = get_fst_file(this.selfData.src);
            target = fs.readFileSync(file);
        }
        return hash(
            target,
            this.selfData.algorithm,
            this.selfData.digestEncoding,
        );
    }
}

module.exports = {
    HashCommand,
};
